package gamagora.game.entity
{
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2FixtureDef;

	public class OSBox extends OSPhysicEntity
	{
		protected var _showDebugBox:Boolean;
		protected var _debugBox:OSEntity;
		
		public function OSBox(type:uint, X:Number=0, Y:Number=0, w:Number = 0, h:Number = 0, physicWidth:Number = 0, physicHeight:Number = 0, SimpleGraphic:Class=null, showDebugBox:Boolean = false)
		{
			super(type, X, Y, SimpleGraphic);
			width = w;
			height = h;
			_physicWidth = physicWidth == 0 ? width : physicWidth;
			_physicHeight = physicHeight == 0 ? height : physicHeight;
			
			//Préparation de la physique
			var boxShape:b2PolygonShape = new b2PolygonShape();
			boxShape.SetAsBox((_physicWidth / 2) / RATIO, (_physicHeight / 2) / RATIO);
			_fixDef.shape = boxShape;
			_bodyDef.position.Set((x + (_physicWidth / 2)) / RATIO, (y + (_physicHeight / 2)) / RATIO);
			
			//Boite de débuggage
			_showDebugBox = showDebugBox;
			if(_showDebugBox)
			{
				_debugBox = new OSEntity(NOTHING, x, y);
				_debugBox.makeGraphic(_physicWidth, _physicHeight, 0x99ff0000);
				add(_debugBox);
			}
		}
		
		public override function update():void
		{
			super.update();
			x = (_body.GetPosition().x * RATIO) - _physicWidth / 2;
			y = (_body.GetPosition().y * RATIO) - _physicHeight / 2;
			angle = _body.GetAngle() * (180 / Math.PI);
			
			if(_debugBox)
			{
				_debugBox.x = x;
				_debugBox.y = y;
				_debugBox.angle = _body.GetAngle() * (180 / Math.PI);
			}
		}
		
		public override function setStartX(decalX:Number):void
		{
			x += decalX;
			_startX += decalX;
			_bodyDef.position.Set((x + (_physicWidth / 2)) / RATIO, (y + (_physicHeight / 2)) / RATIO);
			for(var i:uint=0;i<_nbEntities;i++)
			{
				_entities[i].setStartX(decalX);
			}
		}
	}
}
